package com.itcast.admin.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itcast.admin.domain.Setmeal;
import com.itcast.admin.domain.SetmealCategory;
import com.itcast.admin.domain.request.CreateSetmealCategoryReqDTO;
import com.itcast.admin.domain.request.SetmealCategoryPagerReqDTO;
import com.itcast.admin.domain.response.SetmealCategoryListResDTO;
import com.itcast.admin.domain.response.SetmealCategoryPagerResDTO;
import com.itcast.admin.mapper.SetmealCategoryMapper;
import com.itcast.admin.service.ISetmealCategoryService;
import com.itcast.admin.service.ISetmealService;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.common.utils.ObjectUtils;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 套餐类型表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-11-05
 */
@Service
public class SetmealCategoryServiceImpl extends ServiceImpl<SetmealCategoryMapper, SetmealCategory> implements ISetmealCategoryService {


    @Autowired
    private ISetmealService setmealService;
    /**
     * 分页查询套餐类型
     *
     * @param setmealCategoryPagerReqDTO
     * @return
     */
    @Override
    public PageResult<SetmealCategoryPagerResDTO> pageQuery(SetmealCategoryPagerReqDTO setmealCategoryPagerReqDTO) {
        return PageHelperUtils.selectPage(setmealCategoryPagerReqDTO, () -> baseMapper.pageQuery(setmealCategoryPagerReqDTO));
    }

    /**
     * 新增套餐类型
     *
     * @param createSetmealCategoryReqDTO
     */
    @Override
    public void createSetmealCateGory(CreateSetmealCategoryReqDTO createSetmealCategoryReqDTO) {
        //查询套餐类型是否存在
        SetmealCategory setmealCategory = lambdaQuery().eq(SetmealCategory::getSetmealCategoryName, createSetmealCategoryReqDTO.getSetmealCategoryName()).one();
        if (ObjectUtils.isNotEmpty(setmealCategory)) {
            throw new RuntimeException("该套餐类型已存在");
        }
        SetmealCategory setmealCategoryDB = BeanUtils.copyBean(createSetmealCategoryReqDTO, SetmealCategory.class);
        boolean save = save(setmealCategoryDB);
        if (!save) {
            throw new RuntimeException("保存失败");
        }
    }

    /**
     * 根据id获取套餐类型
     *
     * @param id
     * @return
     */
    @Override
    public SetmealCategoryPagerResDTO getSetmealCateGoryById(Integer id) {
        SetmealCategory setmealCategory = lambdaQuery().eq(SetmealCategory::getId, id).one();
        if (ObjectUtils.isEmpty(setmealCategory)) {
            throw new RuntimeException("套餐类型不存在");
        }
        return BeanUtils.copyBean(setmealCategory, SetmealCategoryPagerResDTO.class);
    }

    /**
     * 根据id修改套餐类型
     *
     * @param id
     * @param createSetmealCategoryReqDTO
     */
    @Override
    public void updateSetmealCateGoryById(Integer id, CreateSetmealCategoryReqDTO createSetmealCategoryReqDTO) {
        List<SetmealCategory> list = lambdaQuery().eq(SetmealCategory::getSetmealCategoryName, createSetmealCategoryReqDTO.getSetmealCategoryName())
                .ne(SetmealCategory::getId, id).list();
        if (list.size() > 0) {
            throw new RuntimeException("该套餐类型已存在");
        }
        //修改套餐类型
        boolean update = lambdaUpdate().set(SetmealCategory::getSetmealCategoryName, createSetmealCategoryReqDTO.getSetmealCategoryName())
                .set(SetmealCategory::getSetmealCategorySortNum, createSetmealCategoryReqDTO.getSetmealCategorySortNum())
                .eq(SetmealCategory::getId, id).update();
        if (!update) {
            throw new RuntimeException("修改套餐类型失败");
        }
    }

    /**
     * 启用套餐类型
     *
     * @param id
     */
    @Override
    public void enableSetmealCateGoryById(Integer id) {
        SetmealCategory setmealCategory = lambdaQuery().eq(SetmealCategory::getId, id).one();
        if (ObjectUtils.isEmpty(setmealCategory)) {
            throw new RuntimeException("套餐类型不存在");
        }
        boolean update = lambdaUpdate().set(SetmealCategory::getSetmealCategoryStatus, 1)
                .eq(SetmealCategory::getId, id).update();
        if (!update) {
            throw new RuntimeException("启用套餐类型失败");
        }
    }

    /**
     * 校验套餐类型是否可以禁用
     *
     * @param id
     */
    @Override
    public String checkDisableById(Integer id) {
        Long count = setmealService.lambdaQuery().eq(Setmeal::getSetmealCategoryId, id)
                .eq(Setmeal::getSetmealStatus, 1).count();
        if (count > 0) {
            return "false";
        }
        return "true";
    }

    /**
     * 根据id禁用套餐类型
     *
     * @param id
     */
    @Override
    public void disableSetmealCateGoryById(Integer id) {
        //判断套餐类型是否关联了启动状态的套餐
        Long count = setmealService.lambdaQuery().eq(Setmeal::getSetmealCategoryId, id)
                .eq(Setmeal::getSetmealStatus, 1).count();
        if (count > 0) {
            throw new RuntimeException("该套餐类型关联了启动状态的套餐，请先禁用关联的套餐");
        }
        boolean update = lambdaUpdate().set(SetmealCategory::getSetmealCategoryStatus, 2)
                .eq(SetmealCategory::getId, id).update();
        if (!update){
            throw new RuntimeException("禁用失败");
        }
    }

    /**
     * 根据id删除套餐类型
     *
     * @param id
     */
    @Override
    public void deleteSetmealCateGoryById(Integer id) {
        SetmealCategory setmealCategory = lambdaQuery().eq(SetmealCategory::getId, id).one();
        if (ObjectUtils.isEmpty(setmealCategory)){
            throw new RuntimeException("套餐类型不存在");
        }
        if (!(setmealCategory.getSetmealCategoryStatus()==0)){
            throw new RuntimeException("该套餐类型状态有误,不能修改");
        }
        boolean removeById = removeById(id);
        if (!removeById){
            throw new RuntimeException("删除失败");
        }
    }

    /**
     * 获取套餐类型下拉列表
     *
     * @param setmealCategoryStatus
     * @return
     */
    @Override
    public List<SetmealCategoryListResDTO> listForFilter(Integer setmealCategoryStatus) {
        List<SetmealCategory> list = lambdaQuery().eq(SetmealCategory::getSetmealCategoryStatus, 1).list();
        if (!list.isEmpty()){
            return BeanUtils.copyList(list, SetmealCategoryListResDTO.class);
        }
        return null;
    }
}
